﻿
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Lines;
using KDS.Mes.Mes.Lines.Dtos;
using KDS.Mes.Mes.Workshops.Dtos;
using KDS.Mes.Web.Controllers;
using KDS.Mes.Web.Models.Lines;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class LinesController : MesControllerBase
    {

        private readonly ILineAppService _lineAppService;

        public LinesController(ILineAppService lineAppService)
        {
            _lineAppService = lineAppService;
           
        }

        [HttpGet]
        [DontWrapResult]
        public JsonResult GetAllLineByWorkshopId(int id)
        {
            List<ComboboxItemDto> lst = new List<ComboboxItemDto>();
            NullableIdDto<int> input = new NullableIdDto<int>();
            input.Id = id;
            lst = _lineAppService.GetLineByWorkshopId(input);
            return Json(lst, JsonRequestBehavior.AllowGet);
        }

        
        [DontWrapResult]
        public JsonResult GetAllLines(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetLineInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _lineAppService.GetPagedLines(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        public  ActionResult Index()
        {
            //var model = new GetLineInput {FilterText = Request.QueryString["filterText"]};
            //       return View(model);
            return View();
        }



        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Basic_Lines_Create, AppPermissions.Pages_Basic_Lines_Edit)]
        public async Task<PartialViewResult> CreateOrEditLineModal(int? id)
        {
		var input=new NullableIdDto<int>{Id=id};
	 
                 var output=    await _lineAppService.GetLineForEditAsync(input);

				 var viewModel=new CreateOrEditLineModalViewModel(output);


            return PartialView("_CreateOrEditLineModal",viewModel);
        }
	 
       
    }
}